<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button icon="el-icon-plus" size="small" type="primary" @click="isShowAddBox=true">新增角色</el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="list">
              <el-table-column type="index" :index="handleIndex" label="序号" width="100" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="handleEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="handleDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页逻辑 -->
            <el-pagination :page-sizes="[2, 3,5,7,9]" :page-size="queryData.pagesize" :current-page="queryData.page" layout="total, slot, sizes, prev, pager, next, jumper" :total="total" @size-change="changeSize" @current-change="changePage" />
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 公司信息 -->
            l-tab-pane>
          </el-tab-pane></el-tabs>
      </el-card>
    </div>
    <el-dialog :title="title" :visible.sync="isShowAddBox" @close="handleClose">
      <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input v-model="form.description" placeholder="请输入角色描述" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="btnCancel">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetRoleList, reqDeleteRole, reqAddRole, reqUpdateRole, reqGetRoleDetail } from '@/api/setting.js'

export default {
  name: 'Setting',
  data() {
    return {
      isShowAddBox: false,
      // 控制加载状态
      loading: false,
      // 查询参数
      queryData: {
        page: 1,
        pagesize: 2
      },
      // 角色列表
      list: [],
      // 列表总数
      total: 0,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name:
           [
             { required: true, message: '请输入角色名称', trigger: ['blur', 'change'] }
           ]

      }
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.loadRoleList()
  },
  methods: {
    //   关闭编辑弹窗
    handleClose() {
      this.$refs.roleForm.resetFields()
      this.form = this.$options.data().form
    },
    // 编辑弹窗,获取数据
    async handleEdit(id) {
      this.isShowAddBox = true
      try {
        const ret = await reqGetRoleDetail(id)
        if (ret.code === 10000) {
          this.form = ret.data
        }
      } catch {
        this.$message.error('获取角色数据失败')
      }
    },

    //   点击取消,关闭弹窗,重置表单
    btnCancel() {
      this.$refs.roleForm.resetFields()
      //   需要多次清空表单的时候用
      this.form = this.$options.data().form
      this.isShowAddBox = false
    },
    //   新增角色
    async handleSubmit() {
      if (this.form.id) {
        // 编辑
        try {
          const ret = await reqUpdateRole(this.form)
          if (ret.code === 10000) {
            this.isShowAddBox = false
            this.loadRoleList()
          }
        } catch {
          this.$message.error('编辑角色失败')
        }
      } else {
        // 添加
        const ret = await reqAddRole(this.form)
        try {
          if (ret.code === 10000) {
            this.loadRoleList()
            this.$message.success('添加角色成功')
            this.isShowAddBox = false
          }
        } catch {
          this.$message.error('添加角色失败')
        }
      }
    },

    //   删除角色
    async handleDel(id) {
      try {
        const ret = await this.$confirm('确认要删除角色吗？, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if (ret === 'cancel') return
        //   点击确定进行删除
        const result = await reqDeleteRole(id)
        if (result.code === 10000) {
        // 删除成功,刷新列表
          this.loadRoleList()
          this.$message.success('删除成功')
        }
      } catch {
        this.$message.error('删除失败')
      }
    },
    //   动态计算分页列表的索引
    handleIndex(index) {
      const { page, pagesize } = this.queryData
      return pagesize * (page - 1) + index + 1
    },
    // 切换每页的条数
    changeSize(size) {
      this.queryData.page = 1
      this.queryData.pagesize = size
      this.loadRoleList()
    },
    // 控制页码的变化
    changePage(page) {
      // page表示当前页码
      this.queryData.page = page
      this.loadRoleList()
    },
    // 获取角色列表数据
    async loadRoleList() {
      try {
        this.loading = true
        const ret = await reqGetRoleList(this.queryData)
        if (ret.code === 10000) {
          this.list = ret.data.rows
          this.total = ret.data.total
        }
      } catch {
        this.$message.error('获取角色列表失败')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
